home *** CD-ROM | disk | FTP | other *** search
- /*
- File: SimpleFormat.c
-
- Copyright (c) 1993-6, Adobe Systems Incorporated.
- All rights reserved.
-
- Example file format module.
- */
-
- #if __MWERKS__
- #include <SetupA4.h> // A4-globals
- #include <A4Stuff.h> // A4-globals
- #endif
-
- #if defined(THINK_C) || defined(__MWERKS__)
- #define ENTRYPOINT main
- #endif
-
- #include "SimpleFormat.h"
-
- Handle hDllInstance = NULL;
-
- /*****************************************************************************/
-
- void InitGlobals (GPtr globals);
- void DoReadPrepare (GPtr globals);
- void DoReadStart (GPtr globals);
- void DoReadContinue (GPtr globals);
- void DoReadFinish (GPtr globals);
- void DoOptionsPrepare (GPtr globals);
- void DoOptionsStart (GPtr globals);
- void DoOptionsContinue (GPtr globals);
- void DoOptionsFinish (GPtr globals);
- void DoEstimatePrepare (GPtr globals);
- void DoEstimateStart (GPtr globals);
- void DoEstimateContinue (GPtr globals);
- void DoEstimateFinish (GPtr globals);
- void DoWritePrepare (GPtr globals);
- void DoWriteStart (GPtr globals);
- void DoWriteContinue (GPtr globals);
- void DoWriteFinish (GPtr globals);
- void DoFilterFile (GPtr globals);
- static Boolean TestAndStoreResult (GPtr globals, OSErr result);
- static Boolean TestAndStoreCancel (GPtr globals, Boolean tocancel);
- static Boolean CheckIdentifier (char identifier []);
- static void SetIdentifier (char identifier []);
- static Boolean CheckForServices (GPtr globals);
- static int32 RowBytes (GPtr globals);
- static Boolean AllocatePixelBuffer (GPtr globals);
- static void DisposePixelBuffer (GPtr globals);
- static void ReadSome (GPtr globals, int32 count, void *buffer);
- static void WriteSome (GPtr globals, int32 count, void *buffer);
- static void ReadRow (GPtr globals);
- static void WriteRow (GPtr globals);
- static void DisposeImageResources (GPtr globals);
-
- /*****************************************************************************/
-
- /* Main dispatching routine. Initializes and sets up the global variables,
- and performs the operation specified by the selector. */
-
- #if MSWindows
- void ENTRYPOINT (short selector,
- FormatRecordPtr formatParamBlock,
- long *data,
- short *result)
- #else
- pascal void ENTRYPOINT (short selector,
- FormatRecordPtr formatParamBlock,
- long *data,
- short *result)
- #endif
- {
-
- /* Set up the globals. */
-
- Globals globalValues;
- GPtr globals = &globalValues;
-
- #if __MWERKS__
- EnterCodeResource(); // A4-globals
- #endif
-
- if (!*data)
- {
-
- InitGlobals (globals);
-
- *data = (long) NewHandle (sizeof (Globals));
-
- if (!*data)
- {
- *result = memFullErr;
- return;
- }
-
- ** (GHdl) *data = globalValues;
-
- }
-
- globalValues = ** (GHdl) *data;
-
- /* Perform the requested operation */
-
- gStuff = formatParamBlock;
- gResult = noErr;
-
- switch (selector)
- {
-
- case formatSelectorAbout:
- DoAbout (globals);
- break;
-
- case formatSelectorReadPrepare:
- DoReadPrepare (globals);
- break;
-
- case formatSelectorReadStart:
- DoReadStart (globals);
- break;
-
- case formatSelectorReadContinue:
- DoReadContinue (globals);
- break;
-
- case formatSelectorReadFinish:
- DoReadFinish (globals);
- break;
-
- case formatSelectorOptionsPrepare:
- DoOptionsPrepare (globals);
- break;
-
- case formatSelectorOptionsStart:
- DoOptionsStart (globals);
- break;
-
- case formatSelectorOptionsContinue:
- DoOptionsContinue (globals);
- break;
-
- case formatSelectorOptionsFinish:
- DoOptionsFinish (globals);
- break;
-
- case formatSelectorEstimatePrepare:
- DoEstimatePrepare (globals);
- break;
-
- case formatSelectorEstimateStart:
- DoEstimateStart (globals);
- break;
-
- case formatSelectorEstimateContinue:
- DoEstimateContinue (globals);
- break;
-
- case formatSelectorEstimateFinish:
- DoEstimateFinish (globals);
- break;
-
- case formatSelectorWritePrepare:
- DoWritePrepare (globals);
- break;
-
- case formatSelectorWriteStart:
- DoWriteStart (globals);
- break;
-
- case formatSelectorWriteContinue:
- DoWriteContinue (globals);
- break;
-
- case formatSelectorWriteFinish:
- DoWriteFinish (globals);
- break;
-
- case formatSelectorFilterFile:
- DoFilterFile (globals);
- break;
-
- default:
- gResult = formatBadParameters;
- }
-
- *result = gResult;
- ** (GHdl) *data = globalValues;
-
- #if __MWERKS__
- ExitCodeResource(); // A4-globals
- #endif
-
- }
-
- /*****************************************************************************/
-
- /* Sets the global variables to their default values. */
-
- void InitGlobals (GPtr globals)
- {
-
- Ptr p;
- int16 count;
-
- p = (Ptr) globals;
- count = sizeof(Globals);
-
- while (count--)
- *p++ = 0;
-
- }
-
- /*****************************************************************************/
-
- static Boolean CheckIdentifier (char identifier [])
- {
-
- return identifier[0] == 'o' &&
- identifier[1] == 'n' &&
- identifier[2] == 'e' &&
- identifier[3] == 'b' &&
- identifier[4] == 'r' &&
- identifier[5] == 'a' &&
- identifier[6] == 'i' &&
- identifier[7] == 'n';
-
- }
-
- /*****************************************************************************/
-
- static void SetIdentifier (char identifier [])
- {
-
- identifier[0] = 'o';
- identifier[1] = 'n';
- identifier[2] = 'e';
- identifier[3] = 'b';
- identifier[4] = 'r';
- identifier[5] = 'a';
- identifier[6] = 'i';
- identifier[7] = 'n';
-
- }
-
- /*****************************************************************************/
-
- static Boolean CheckForServices (GPtr globals)
- {
-
- return WarnBufferProcsAvailable () &&
- WarnAdvanceStateAvailable () &&
- WarnHandleProcsAvailable ();
-
- }
-
- /*****************************************************************************/
-
- static Boolean TestAndStoreResult (GPtr globals, OSErr result)
- {
-
- if (result != noErr)
- {
- if (gResult == noErr)
- gResult = result;
- return FALSE;
- }
- else
- return TRUE;
-
- }
-
- #define TSR(x) TestAndStoreResult (globals, x)
-
- /*****************************************************************************/
-
- static Boolean TestAndStoreCancel (GPtr globals, Boolean tocancel)
- {
-
- if (tocancel)
- {
- if (gResult == noErr)
- gResult = userCanceledErr;
- return FALSE;
- }
- else
- return TRUE;
-
- }
-
- #define TSC(x) TestAndStoreCancel (globals, x)
-
- /*****************************************************************************/
-
- static int32 RowBytes (GPtr globals)
- {
-
- return (gStuff->imageSize.h * (int32) gStuff->depth + 7) >> 3;
-
- }
-
- /*****************************************************************************/
-
- static Boolean AllocatePixelBuffer (GPtr globals)
- {
-
- BufferID buffer;
-
- if (gResult != noErr) return FALSE;
-
- /* We will want a buffer that is one line wide. */
-
- gPixelBuffer = 0;
-
- gRowBytes = RowBytes (globals);
-
- if (!TSR (AllocateBuffer (gRowBytes, &buffer))) return FALSE;
-
- gPixelBuffer = buffer;
-
- gPixelData = LockBuffer (gPixelBuffer, FALSE);
-
- return TRUE;
-
- }
-
- /*****************************************************************************/
-
- static void DisposePixelBuffer (GPtr globals)
- {
-
- if (gPixelBuffer != 0)
- {
-
- FreeBuffer (gPixelBuffer);
-
- gPixelBuffer = 0;
- gPixelData = 0;
-
- }
-
- }
-
- /*****************************************************************************/
-
- void DoReadPrepare (GPtr globals)
- {
-
- gStuff->maxData = 0;
-
- }
-
- /*****************************************************************************/
-
- static void ReadSome (GPtr globals, int32 count, void *buffer)
- {
-
- int32 readCount = count;
-
- if (gResult != noErr)
- return;
-
- gResult = FSRead (gStuff->dataFork, &readCount, buffer);
-
- if (gResult == noErr && readCount != count)
- gResult = eofErr;
-
- }
-
- /*****************************************************************************/
-
- static void WriteSome (GPtr globals, int32 count, void *buffer)
- {
-
- int32 writeCount = count;
-
- if (gResult != noErr)
- return;
-
- gResult = FSWrite (gStuff->dataFork, &writeCount, buffer);
-
- if (gResult == noErr && writeCount != count)
- gResult = dskFulErr;
-
- }
-
- /*****************************************************************************/
-
- static void ReadRow (GPtr globals)
- {
-
- ReadSome (globals, gRowBytes, gPixelData);
-
- }
-
- /*****************************************************************************/
-
- static void WriteRow (GPtr globals)
- {
-
- WriteSome (globals, gRowBytes, gPixelData);
-
- }
-
- /*****************************************************************************/
-
- static void DisposeImageResources (GPtr globals)
- {
-
- if (gStuff->imageRsrcData)
- {
-
- PIDisposeHandle (gStuff->imageRsrcData);
-
- gStuff->imageRsrcData = NULL;
-
- gStuff->imageRsrcSize = 0;
-
- }
-
- }
-
- /*****************************************************************************/
-
- void DoReadStart (GPtr globals)
- {
-
- FileHeader header;
- Boolean servicesAvailable = false;
-
- ReadScriptParamsOnRead (globals); // override params here
-
- /* Exit if we have already encountered an error. */
-
- if (gResult != noErr) return;
-
- /* Check for the needed services. */
-
- if (!TSC ((Boolean)(!CheckForServices(globals))) ) return;
-
- /* If we have not encountered an error, then we want to read
- the file header. */
-
- if (!TSR (SetFPos (gStuff->dataFork, fsFromStart, 0))) return;
-
- ReadSome (globals, sizeof (FileHeader), &header);
-
- if (gResult != noErr) return;
-
- if (!CheckIdentifier (header.identifier))
- {
- gResult = formatCannotRead;
- return;
- }
-
- gStuff->imageMode = header.mode;
- gStuff->imageSize.v = header.rows;
- gStuff->imageSize.h = header.cols;
- gStuff->depth = header.depth;
- gStuff->planes = header.planes;
-
- /* Next, we will try to read the image resources. */
-
- if (header.resourceLength > 0)
- {
-
- gStuff->imageRsrcData = PINewHandle (header.resourceLength);
-
- if (!gStuff->imageRsrcData)
- {
- gResult = memFullErr;
- return;
- }
-
- ReadSome (globals,
- header.resourceLength,
- PILockHandle (gStuff->imageRsrcData, FALSE));
-
- PIUnlockHandle (gStuff->imageRsrcData);
-
- if (gResult != noErr)
- goto CleanUp;
-
- gStuff->imageRsrcSize = header.resourceLength;
-
- }
-
- /* Next, we will will read the lookup tables if in indexed color mode. */
-
- if (header.mode == plugInModeIndexedColor)
- {
-
- ReadSome (globals, 3 * sizeof (LookUpTable), &gStuff->redLUT);
-
- if (gResult != noErr)
- goto CleanUp;
-
- }
-
- return;
-
- /* The following code does any clean-up work in the event of an error. */
-
- CleanUp:
-
- DisposeImageResources (globals);
-
- }
-
- /*****************************************************************************/
-
- void DoReadContinue (GPtr globals)
- {
-
- int32 done;
- int32 total;
- int16 plane;
- int16 row;
-
- /* Dispose of the image resource data if it exists. */
-
- DisposeImageResources (globals);
-
- /* Set up the progress variables. */
-
- done = 0;
- total = gStuff->imageSize.v * (long) gStuff->planes;
-
- /* Next, we will allocate the pixel buffer. */
-
- AllocatePixelBuffer (globals);
-
- /* Set up to start returning chunks of data. */
-
- gStuff->theRect.left = 0;
- gStuff->theRect.right = gStuff->imageSize.h;
- gStuff->colBytes = 1;
- gStuff->rowBytes = gRowBytes;
- gStuff->planeBytes = 0;
- gStuff->data = gPixelData;
-
- for (plane = 0; gResult == noErr && plane < gStuff->planes; ++plane)
- {
-
- gStuff->loPlane = gStuff->hiPlane = plane;
-
- for (row = 0; gResult == noErr && row < gStuff->imageSize.v; ++row)
- {
-
- gStuff->theRect.top = row;
- gStuff->theRect.bottom = row + 1;
-
- ReadRow (globals);
-
- if (gResult == noErr)
- gResult = AdvanceState ();
-
- UpdateProgress (++done, total);
-
- }
-
- }
-
- gStuff->data = NULL;
-
- DisposePixelBuffer (globals);
-
- }
-
- /*****************************************************************************/
-
- void DoReadFinish (GPtr globals)
- {
-
- /* Dispose of the image resource data if it exists. */
-
- DisposeImageResources (globals);
- WriteScriptParamsOnRead (globals); // should be different for read/write
-
- }
-
- /*****************************************************************************/
-
- void DoOptionsPrepare (GPtr globals)
- {
-
- gStuff->maxData = 0;
-
- }
-
- /*****************************************************************************/
-
- void DoOptionsStart (GPtr globals)
- {
-
- /* Check for the needed services. */
-
- if (!TSC ((Boolean)(!CheckForServices (globals)))) return;
-
- gStuff->data = NULL;
-
- }
-
- /*****************************************************************************/
-
- void DoOptionsContinue (GPtr globals)
- {
-
- }
-
- /*****************************************************************************/
-
- void DoOptionsFinish (GPtr globals)
- {
-
- }
-
- /*****************************************************************************/
-
- void DoEstimatePrepare (GPtr globals)
- {
-
- gStuff->maxData = 0;
-
- }
-
- /*****************************************************************************/
-
- void DoEstimateStart (GPtr globals)
- {
-
- int32 dataBytes;
-
- /* Check for the needed services. */
-
- if (!TSC ((Boolean)(!CheckForServices (globals)))) return;
-
- dataBytes = sizeof (FileHeader) +
- gStuff->imageRsrcSize +
- RowBytes (globals) * gStuff->planes * gStuff->imageSize.v;
-
- if (gStuff->imageMode == plugInModeIndexedColor)
- dataBytes += 3 * sizeof (LookUpTable);
-
- gStuff->minDataBytes = dataBytes;
- gStuff->maxDataBytes = dataBytes;
-
- gStuff->data = NULL;
-
- }
-
- /*****************************************************************************/
-
- void DoEstimateContinue (GPtr globals)
- {
-
- }
-
- /*****************************************************************************/
-
- void DoEstimateFinish (GPtr globals)
- {
-
- }
-
- /*****************************************************************************/
-
- void DoWritePrepare (GPtr globals)
- {
-
- gStuff->maxData = 0;
-
- }
-
- /*****************************************************************************/
-
- void DoWriteStart (GPtr globals)
- {
-
- FileHeader header;
- int32 done;
- int32 total;
- int16 plane;
- int16 row;
-
- ReadScriptParamsOnWrite (globals); // read script params here
-
- if (gResult != noErr) return;
-
- /* Check for the needed services. */
-
- if (!TSC ((Boolean)(!CheckForServices (globals)))) return;
-
- /* Write the header. */
-
- gResult = SetFPos (gStuff->dataFork, fsFromStart, 0);
-
- SetIdentifier (header.identifier);
- header.mode = gStuff->imageMode;
- header.rows = gStuff->imageSize.v;
- header.cols = gStuff->imageSize.h;
- header.depth = gStuff->depth;
- header.planes = gStuff->planes;
-
- if (gStuff->imageRsrcData)
- header.resourceLength = gStuff->imageRsrcSize;
- else
- header.resourceLength = 0;
-
- WriteSome (globals, sizeof (FileHeader), &header);
-
- if (gResult != noErr) return;
-
- /* Write the image resources if any. */
-
- if (header.resourceLength > 0)
- {
-
- WriteSome (globals,
- header.resourceLength,
- PILockHandle (gStuff->imageRsrcData, FALSE));
-
- PIUnlockHandle (gStuff->imageRsrcData);
-
- if (gResult != noErr) return;
-
- }
-
- /* Write the lookup tables if appropriate. */
-
- if (header.mode == plugInModeIndexedColor)
- {
-
- WriteSome (globals, 3 * sizeof (LookUpTable), &gStuff->redLUT);
-
- if (gResult != noErr) return;
-
- }
-
- /* Set up the progress variables. */
-
- done = 0;
- total = header.rows * (long) header.planes;
-
- /* Next, we will allocate the pixel buffer. */
-
- AllocatePixelBuffer (globals);
-
- /* Set up to start receiving chunks of data. */
-
- gStuff->theRect.left = 0;
- gStuff->theRect.right = header.cols;
- gStuff->colBytes = 1;
- gStuff->rowBytes = gRowBytes;
- gStuff->planeBytes = 0;
- gStuff->data = gPixelData;
-
- for (plane = 0; gResult == noErr && plane < gStuff->planes; ++plane)
- {
-
- gStuff->loPlane = gStuff->hiPlane = plane;
-
- for (row = 0; gResult == noErr && row < gStuff->imageSize.v; ++row)
- {
-
- gStuff->theRect.top = row;
- gStuff->theRect.bottom = row + 1;
-
- if (gResult == noErr)
- gResult = AdvanceState ();
-
- WriteRow (globals);
-
- UpdateProgress (++done, total);
-
- }
-
- }
-
- gStuff->data = NULL;
-
- DisposePixelBuffer (globals);
-
- }
-
- /*****************************************************************************/
-
- void DoWriteContinue (GPtr globals)
- {
-
- }
-
- /*****************************************************************************/
-
- void DoWriteFinish (GPtr globals)
- {
- WriteScriptParamsOnWrite (globals); // should be different for read/write
- }
-
- /*****************************************************************************/
-
- void DoFilterFile (GPtr globals)
- {
-
- FileHeader header;
-
- /* Exit if we have already encountered an error. */
-
- if (gResult != noErr) return;
-
- /* Read the file header. */
-
- if (!TSR (SetFPos (gStuff->dataFork, fsFromStart, 0))) return;
-
- ReadSome (globals, sizeof (FileHeader), &header);
-
- if (gResult != noErr) return;
-
- /* Check the identifier. */
-
- if (!CheckIdentifier (header.identifier))
- {
- gResult = formatCannotRead;
- return;
- }
-
- }
-
- /*****************************************************************************/
-
-
-
-
-